<template>
  <a-form @submit="handleSubmit" :form="form" class="form">
    <a-row class="form-row" :gutter="16">
      <a-col :lg="6" :md="12" :sm="24">
        <a-form-item label="$t('userRepositoryForm.ckm')">
          <a-input
            placeholder="$t('userRepositoryForm.qsrckmc')"
            v-decorator="[
              'name',
              {rules: [{ required: true, message: '{{ $t('userRepositoryForm.qsrckmc') }}', whitespace: true}]}
            ]" />
        </a-form-item>
      </a-col>
      <a-col :xl="{span: 7, offset: 1}" :lg="{span: 8}" :md="{span: 12}" :sm="24">
        <a-form-item
          label="$t('userRepositoryForm.ckym')">
          <a-input
            addonBefore="http://"
            addonAfter=".com"
            placeholder="$t('userRepositoryForm.qsr')"
            v-decorator="[
              'url',
              {rules: [{ required: true, message: '{{ $t('userRepositoryForm.qsrckym') }}', whitespace: true}, {validator: validate}]}
            ]" />
        </a-form-item>
      </a-col>
      <a-col :xl="{span: 9, offset: 1}" :lg="{span: 10}" :md="{span: 24}" :sm="24">
        <a-form-item
          label="$t('userRepositoryForm.ckgly')">
          <a-select placeholder="$t('userRepositoryForm.qxzgly')" v-decorator="[ 'owner', {rules: [{ required: true, message: '$t('userRepositoryForm.qxzgly')'}]} ]">
            <a-select-option value="{{ $t('userRepositoryForm.wtx') }}">{{ $t('userRepositoryForm.wtx') }}</a-select-option>
            <a-select-option value="{{ $t('userRepositoryForm.ltx') }}">{{ $t('userRepositoryForm.ltx') }}</a-select-option>
            <a-select-option value="{{ $t('userRepositoryForm.htx') }}">{{ $t('userRepositoryForm.htx') }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
    </a-row>
    <a-row class="form-row" :gutter="16">
      <a-col :lg="6" :md="12" :sm="24">
        <a-form-item
          label="$t('userRepositoryForm.spr')">
          <a-select placeholder="$t('userRepositoryForm.qxzspy')" v-decorator="[ 'approver', {rules: [{ required: true, message: '$t('userRepositoryForm.qxzspy')'}]} ]">
            <a-select-option value="{{ $t('userRepositoryForm.wxl') }}">{{ $t('userRepositoryForm.wxl') }}</a-select-option>
            <a-select-option value="{{ $t('userRepositoryForm.lj') }}">{{ $t('userRepositoryForm.lj') }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      <a-col :xl="{span: 7, offset: 1}" :lg="{span: 8}" :md="{span: 12}" :sm="24">
        <a-form-item
          label="$t('userRepositoryForm.sxrq')">
          <a-range-picker
            style="width: 100%"
            v-decorator="[
              'dateRange',
              {rules: [{ required: true, message: '{{ $t('userRepositoryForm.qxzsxrq') }}'}]}
            ]" />
        </a-form-item>
      </a-col>
      <a-col :xl="{span: 9, offset: 1}" :lg="{span: 10}" :md="{span: 24}" :sm="24">
        <a-form-item
          label="$t('userRepositoryForm.cklx')">
          <a-select
            placeholder="$t('userRepositoryForm.qxzcklx')"
            v-decorator="[
              'type',
              {rules: [{ required: true, message: '{{ $t('userRepositoryForm.qxzcklx') }}'}]}
            ]" >
            <a-select-option value="{{ $t('userRepositoryForm.gk') }}">{{ $t('userRepositoryForm.gk') }}</a-select-option>
            <a-select-option value="{{ $t('userRepositoryForm.sm') }}">{{ $t('userRepositoryForm.sm') }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
    </a-row>
    <a-form-item v-if="showSubmit">
      <a-button htmlType="submit" >Submit</a-button>
    </a-form-item>
  </a-form>
</template>

<script>
export default {
  name: 'RepositoryForm',
  props: {
    showSubmit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: this.$form.createForm(this)
    }
  },
  methods: {
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.$notification['error']({
            message: 'Received values of form:',
            description: values
          })
        }
      })
    },
    validate (rule, value, callback) {
      const regex = /^user-(.*)$/
      if (!regex.test(value)) {
        callback(new Error('需要以 user- 开头'))
      }
      callback()
    }
  }
}
</script>

<style scoped>

</style>
